package doinbackground;

import core.db.DbHelper;
import core.db.sqlite.Selector;
import core.db.util.TextUtils;
import core.generatingcode.GeneratingJava;
import core.swagger.AnalysisSwagger;
import core.task.async.bean.Param;
import core.task.async.bean.Result;
import core.task.async.callback.IWorker;
import core.task.async.wrapper.WorkerWrapper;
import entity.ApiAction;
import service.ProcessService;

import java.util.List;
import java.util.Map;

public class GeneratingJavaTask implements IWorker<Param, Result> {

    @Override
    public Result action(Param param, Map<String, WorkerWrapper> allWrappers) {
        String method = param.get();
        Object data = param.get("data");
        String resRoot = param.get("resRoot");
        if (!"generateCode".equals(method)) {
            return ProcessService.error(method, 0, "非generateCode接口!");
        }
        if(onCallback==null){
            return ProcessService.error(method, 0, "回调为空!");
        }
        String id = data.toString();
        if(!TextUtils.isEmpty(id)){
            ApiAction apiAction = DbHelper.findById(ApiAction.class,id);
            if(apiAction==null){
                return ProcessService.error(method,-1,"不存在此数据");
            }
             GeneratingJava.generating(resRoot,apiAction,onCallback);
        }else{
            List<ApiAction> allApis = DbHelper.findAll(Selector.from(ApiAction.class).where("isDel","=",false));
            GeneratingJava.generating(resRoot,allApis,onCallback);
        }
        return null;
    }
    private GeneratingJava.OnCallback onCallback;

    public GeneratingJava.OnCallback getOnCallback() {
        return onCallback;
    }

    public void setOnCallback(GeneratingJava.OnCallback onCallback) {
        this.onCallback = onCallback;
    }
}
